package ipad.asia.controller;


import com.alibaba.fastjson.JSON;
import ipad.asia.config.NewReason;
import ipad.asia.entity.Consumer;
import ipad.asia.entity.Order;
import ipad.asia.entity.Product;
import ipad.asia.entity.PunishConsumer;
import ipad.asia.service.consumer.ConsumerFeign;
import ipad.asia.service.order.OrderFeign;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.service.punish.PunishConsumerFeign;
import ipad.asia.service.punish.PunishEnterpriseFeign;
import ipad.asia.service.shop.ShopFeign;
import ipad.asia.utils.FtpUtil;
import ipad.asia.utils.MyTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ReportController {
    /**
     * 这是一个举报
     */
    @Autowired
    PunishConsumerFeign punishConsumerFeign;
    @Autowired
    private MyTime myTime;
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private ConsumerFeign consumerFeign;
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private ShopFeign shopFeign;
    @RequestMapping("/shop/report")
    public String report(@RequestParam(value = "orderId",required = false)String orderId,
                         @RequestParam(value = "type",required = false)String type,
                         @RequestParam(value = "reason",required = false)String reason,
                         @RequestParam(value = "proof",required = false) MultipartFile proof,
                         @RequestParam(value = "email",required = false)String reportEmail,
                         Model model, HttpSession session) throws IOException {
        Order order=orderFeign.findId(Integer.parseInt(orderId));
        String consumerName = consumerFeign.getId(order.getConsumerId()).getUser();
        String productName = productFeign.getById(order.getProductId()).getName();
        if(reason==null){
//            Consumer consumer=consumerFeign.getName((String) session.getAttribute("user"));
//            List<Order> orders = orderFeign.findByConsumerId(consumer.getId());
//            List<Product> products = productFeign.all();
//            List<> orderVOS=new ArrayList<>();
//            for(int i=0;i<orders.size();i++){
//                for(int j=0;j<products.size();j++){
//                    if(orders.get(i).getProductId()==products.get(i).getId()){
//                        OrderVO orderVO=new OrderVO();
//                        orderVO.setOrder(orders.get(i));
//                        orderVO.setProduct(products.get(j));
//                        orderVOS.add(orderVO);
//                    }
//                }
//            }
//            model.addAttribute("orderVO",orderVOS);
            model.addAttribute("consumerName",consumerName);
            model.addAttribute("productName",productName);
            model.addAttribute("orderId",orderId);
            model.addAttribute("message","请填写举报理由");
            return "report";
        }
        System.out.println("类型："+type);
        //不想改动数据库，使用JSON
        int consumeId=consumerFeign.getName(consumerName).getId();
        NewReason reasonVO=new NewReason();
        FtpUtil.uploadFile(proof.getOriginalFilename(),proof.getInputStream());
        reasonVO.setReason(reason);reasonVO.setType(type);reasonVO.setOrderId(Integer.parseInt(orderId));
        punishConsumerFeign.complaintConsumer(consumeId,consumerName,JSON.toJSONString(reasonVO),proof.getOriginalFilename(),reportEmail,myTime.LocalDateTime());
        model.addAttribute("message","举报成功!");
        return "report";
    }
}
